#include "notifynotification.h"

#include <QTimer>


namespace Notify {

Notification::Notification(QObject *parent) :
    QObject(parent),
    m_defaultCallbackTimer(0),
    m_referenceCount(0),
    m_closing(false),
    m_defaulTimeout(5*1000)
{
}


Notification::~Notification()
{

}


void Notification::acquire()
{
    m_referenceCount++;
}


void Notification::close()
{
    if (!m_closing) {
        m_closing = true;
        emit closed(this);
    }
}


void Notification::release()
{
    m_referenceCount--;
    if (m_referenceCount <= 0) {
        close();
    }
}


void Notification::setDefaultCallback()
{
    if (m_defaulTimeout <= 0)
        return;

    m_defaultCallbackTimer = new QTimer(this);
    m_defaultCallbackTimer->setSingleShot(true);
    connect(m_defaultCallbackTimer, &QTimer::timeout,
            this, &Notification::onTimeOut);
    m_defaultCallbackTimer->start(m_defaulTimeout);
}


void Notification::setDefaultTimeout(int timeout)
{
    m_defaulTimeout = timeout;
}


void Notification::onTimeOut()
{
    emit timeout();
}

} // namespace Notify
